//
//  CatogryArtifactViewController.m
//  project
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryArtifactViewController.h"
#import "HotListCell.h"
#import "PKRequestManager.h"
#import "DOPDropDownMenu.h"
#import "CatogryArtifactDetailViewController.h"
#import "MJRefresh.h"

@interface CatogryArtifactViewController () <UICollectionViewDataSource,UICollectionViewDelegate,DOPDropDownMenuDataSource,DOPDropDownMenuDelegate>

//创建表头上的四个button
//对象
@property(nonatomic,strong)UIButton *objectButton;
//场合
@property(nonatomic,strong)UIButton *occasionButton;
//个性
@property(nonatomic,strong)UIButton *personalityButton;
//价格
@property(nonatomic,strong)UIButton *priceButton;

//创建一个collectionView
@property(nonatomic,strong)UICollectionView *artifactCollectionView;

//创建一个接收model数据的数组
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

//创建第三方的分类的数组
@property(nonatomic,strong)NSArray *objectArray;
@property(nonatomic,strong)NSArray *occasionArray;
@property(nonatomic,strong)NSArray *personalityArray;
@property(nonatomic,strong)NSArray *priceArray;

//创建第三方属性
@property(nonatomic,strong)DOPDropDownMenu *menu;

//定义一个接收第三方网址的string
@property(nonatomic,strong)NSString *str;

@end

@implementation CatogryArtifactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 40 ) / 2, 10, 40, 20)];
    titleLabel.text = @"选礼神器";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    
    _objectArray = @[@"对象",@"男票",@"女盆友",@"闺蜜们",@"基友",@"爸爸妈妈",@"小盆友",@"同事"];
    _occasionArray = @[@"场合",@"生日",@"情人节",@"结婚",@"新年",@"感谢",@"纪念日",@"乔迁",@"圣诞节"];
    _personalityArray = @[@"个性",@"萌",@"小清新",@"创意",@"奇葩",@"文艺范",@"科技感",@"设计感"];
    _priceArray = @[@"价格",@"50以下",@"50-200",@"200-500",@"500-1000",@"1000以上"];
    
    //添加下拉菜单
    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc]initWithOrigin:CGPointMake(0, 64) andHeight:44];
    menu.delegate = self;
    menu.dataSource = self;
    [self.view addSubview:menu];
    _menu = menu;
    
    //让原本请求的网址给属性
    _str = @"http://api.liwushuo.com/v2/search/item_by_type?limit=20&offset=0";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    CGFloat item = (kWidth - 30)/2;
    layout.itemSize = CGSizeMake(item, item + 80);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    _artifactCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 110, kWidth, kHeight - 110) collectionViewLayout:layout];
    _artifactCollectionView.backgroundColor = [UIColor whiteColor];
    _artifactCollectionView.dataSource = self;
    _artifactCollectionView.delegate = self;
    [_artifactCollectionView registerClass:[HotListCell class] forCellWithReuseIdentifier:@"HotListCell"];
    _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    [self.view addSubview:_artifactCollectionView];
    
    [self requestData];
    
    //上拉加载
    __weak CatogryArtifactViewController *newSelf = self;
    self.artifactCollectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [newSelf requestPullData];
    }];
}

-(NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 4;
}

-(NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if (column == 0)
    {
        return _objectArray.count;
    }
    else if (column == 1)
    {
        return _occasionArray.count;
    }
    else if (column == 2)
    {
        return _personalityArray.count;
    }
    else
    {
        return _priceArray.count;
    }
}

-(NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0)
    {
        return _objectArray[indexPath.row];
    }
    else if (indexPath.column == 1)
    {
        return _occasionArray[indexPath.row];
    }
    else if (indexPath.column == 2)
    {
        return _personalityArray[indexPath.row];
    }
    else
    {
        return _priceArray[indexPath.row];
    }
}

-(void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotListCell" forIndexPath:indexPath];
    [cell ConfigModel: _dataSourceArray[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CatogryArtifactDetailViewController *artifactDetailVC = [[CatogryArtifactDetailViewController alloc]init];
    artifactDetailVC.model = _dataSourceArray[indexPath.row];
    [self.navigationController pushViewController:artifactDetailVC animated:YES];
}

//请求数据
-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item_by_type?limit=20&offset=%d",offset] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            ListModel *model = [[ListModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        
        [_artifactCollectionView reloadData];
        
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

//上拉加载
static int offset = 0;
-(void)requestPullData
{
    offset = offset + 20;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:_str parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            ListModel *model = [[ListModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        
        [_artifactCollectionView reloadData];
        [_artifactCollectionView.footer endRefreshing];
        
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
